package com.skyworth.web.common.service.serviceImpl;


import com.skyworth.web.common.domain.AttachmentModel;
import com.skyworth.web.common.form.AttachmentForm;
import com.skyworth.web.common.repository.AttachmentRepository;
import com.skyworth.web.common.service.AttachmentService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by skyworth on 2016/1/21.
 */
@Service
public class AttachmentServiceImpl implements AttachmentService {
    Logger LOG = LoggerFactory.getLogger(AttachmentServiceImpl.class);

    @Autowired
    private AttachmentRepository attachRepository;

    @Override
    public void saveAttachs(ArrayList<AttachmentForm> attaches) {

        for(AttachmentForm attach: attaches){
            AttachmentModel attachMode = new AttachmentModel();
            attachMode.name = attach.name;
            attachMode.url = attach.url;
            attachMode.userId = attach.userId;

            attachRepository.save(attachMode);
        }
    }

    @Override
    public AttachmentModel saveAttach(AttachmentForm attachmentForm) {
        AttachmentModel attachMode = new AttachmentModel();
        attachMode.name = attachmentForm.name;
        attachMode.url = attachmentForm.url;
        attachMode.userId = attachmentForm.userId;

        attachRepository.save(attachMode);
        return attachMode;
    }

    @Override
    public void delAttachment(long id) {
        attachRepository.delete(id);
    }

    @Override
    public AttachmentModel findAttachment(long id) {
        return attachRepository.findOne(id);
    }

    @Override
    public String getAttachmentUrl(long id) {
        AttachmentModel attach = this.findAttachment(id);
        if(attach!=null)
        {
            return attach.getUrl();
        }else {
            return null;
        }
    }

    @Override
    public List<AttachmentModel> getUserCertification(long user_id) {
        return attachRepository.findByUserId(user_id);
    }
}
